package com.cloud.salon.common.crud.service.impl;

import com.cloud.salon.common.crud.service.CRUDService;
import com.cloud.salon.common.base.response.Res;
import com.cloud.salon.common.base.entity.BaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;


import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Description Service基础实现类
 * @Author wolf--LZH
 * @CreateDate 2019/3/24 21:59
 */

public class CRUDServiceImpl<T extends BaseEntity, R extends JpaRepository<T, Integer>> implements CRUDService<T> {
    @Autowired
    private R r;

    /*
     * @Description 通过id获取实体
     * @Author wolf--LZH
     */
    @Override
    public Res<T> findById(Integer id) {
        if (id == null)
            return new Res(-3, "id为null");
        Optional<T> optional = r.findById(id);
        if(optional.isPresent())
            return new Res(0, "获取成功", optional.get());
        else
            return new Res(-3, "获取失败,无此实体");
    }

    /*
     * @Description 获取所有数据
     * @Author wolf--LZH
     */
    public Res<List<T>> findAll() {
        return new Res(0, "查询成功", r.findAll());
    }

    /*
     * @Description 保存实体
     * @Author wolf--LZH
     */
    @Override
    public Res<T> save(T t) {
        T temp = r.save(t);
        if(temp != null)
            return new Res(0, "添加成功", temp);
        return  new Res(-3, "添加失败,请稍后再试");
    }

    /*
     * @Description 批量添加数据
     * @Author wolf--LZH
     */
    @Override
    public Res<List<T>> saveAll(List<T> entitys) {
        List<T> res = new ArrayList<T>();
        List<T> data = new ArrayList<>();
        int count = 0;
        for (T entity: entitys) {
            if (count >= 100) {
                res.addAll(r.saveAll(data));
                data.clear();
                count = 0;
            }
            data.add(entity);
            count++;
        }
        res.addAll(r.saveAll(data));

        return new Res(0, "成功添加" + res.size() + "条数据", res);
    }

    /*
     * @Description 更新某实体
     * @Author wolf--LZH
     */
    @Override
    public Res<T> update(T t) {
        //如果不存在该实体，不准更新数据库，并返回null
        if (findById(t.getId()).getData() == null) {
            return new Res(-3, "不存在此实体,请确认后再修改");
        }
        return new Res(0, "修改成功", r.save(t));
    }

    /*
     * @Description 批量修改实体
     * @Author wolf--LZH
     */
    @Override
    public Res<List<T>> updateAll(List<T> entitys) {
        List<T> res = new ArrayList<T>();
        for(T entity: entitys) {
            entity = this.update(entity).getData();
            if(entity != null)
                res.add(entity);
        }
        return new Res(0, "成功修改" + res.size() + "条数据", res);
    }

    /*
     * @Description 通过id删除实体
     * @Author wolf--LZH
     */
    @Override
    public Res<Boolean> deleteById(Integer id) {
        try{
            r.deleteById(id);
        }catch(Exception e){
            return new Res(-3, "删除失败,请稍后再试", false);
        }
        return new Res(0, "删除成功", true);
    }

    /*
     * @Description 通过id数组批量删除
     * @Author wolf--LZH
     */
    @Override
    public Res<Integer> deleteByIdAll(List<Integer> ids) {
        int count = 0;
        for(Integer id: ids) {
            if(this.deleteById(id).getData())
                count++;
        }
        return new Res(0, "成功删除" + count + "条数据", count);
    }
}
